import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) throws SQLException {
        Scanner in = new Scanner(System.in);
        int id = in.nextInt();
        String name = in.next();

        // 1. 先创建一个 DataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("cjc123456");

        // 2. 与数据库服务器建立连接, 方便后续进行  请求-响应交互
        Connection connection = dataSource.getConnection();

        // 3. 构造 sql 语句，不用sql结尾写;
        String sql = "insert into student (id, name) values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);

        // 4. 把 sql 发给服务器，返回值是一个整数，表示 sql 影响到的行数
        int n = statement.executeUpdate();
        System.out.println(statement);
        System.out.println(n);

        // 5. 释放资源，关闭连接，后获取到的资源先释放
        statement.close();
        connection.close();
    }
}
